package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.ChargingPileLogic;
import cn.topevery.garden.dal.logic.FacilitiesFileLogic;
import cn.topevery.garden.entity.dto.ChargingPileDTO;
import cn.topevery.garden.entity.po.ChargingPile;
import cn.topevery.garden.entity.po.FacilitiesFile;
import cn.topevery.garden.entity.query.ChargingPileQuery;
import cn.topevery.garden.entity.vo.ChargingPileVO;
import cn.topevery.garden.wapi.utils.CoordinateTransHelper;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.dto.PointDto;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Api(tags = "充电桩")
@RestController
@RequestMapping("/chargingPile")
public class ChargingPileController extends BaseController {


    @Autowired
    ChargingPileLogic chargingPileLogic;

    @Autowired
    private Mapper mapper;

    @Autowired
    private FacilitiesFileLogic fileLogic;

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取页列表")
    @PostMapping("/getList")
    public JsonResult<DataPager<ChargingPile>> listPage(@RequestBody ChargingPileQuery query) {
        return JsonResult.ok(chargingPileLogic.listPage(query));
    }

    @GetMapping(value = "/getById")
    @ApiOperation(value = "通过id查询设施")
    public JsonResult<?> getById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        ChargingPile po = chargingPileLogic.getEntity(id);
        List<FacilitiesFile> fileList = fileLogic.getByMainId(po.getFacilitiesId());
        ChargingPileVO vo = mapper.map(po, ChargingPileVO.class);
        vo.setFileList(fileList);
        return JsonResult.ok(vo);
    }

    @GetMapping(value = "/delById")
    @ApiOperation(value = "通过id删除")
    public JsonResult<?> delById(Integer id) {
        if (id == null) {
            return JsonResult.error("id不能为空");
        }
        ChargingPile entity = chargingPileLogic.getEntity(id);
        fileLogic.delByMainId(getUser().id.toString(), entity.getFacilitiesId());
        chargingPileLogic.delete(getUser().id.toString(), id);
        return JsonResult.ok("删除成功");
    }

    @ApiOperation("新增/编辑 信息")
    @PostMapping("/save")
    public JsonResult<?> saveOrUpdate(@Valid @RequestBody ChargingPileDTO param) {
        PointDto pointDto;
        if (param.getLngBd09() != null && param.getLatBd09() != null) {
            pointDto = CoordinateTransHelper.bd09WGS84(param.getLngBd09(), param.getLatBd09());
            param.setLngWgs(pointDto.getLng());
            param.setLatWgs(pointDto.getLat());
            pointDto = CoordinateTransHelper.bdDecrypt(param.getLngBd09(), param.getLatBd09());
            param.setLngGcj02(pointDto.getLng());
            param.setLatGcj02(pointDto.getLat());
            double[] doubles = CoordinateTransHelper.bDWgs2BDMkt(param.getLngBd09(), param.getLatBd09());
            param.setLngBdmkt(doubles[0]);
            param.setLatBdmkt(doubles[1]);
        }
        chargingPileLogic.saveOrUpdate(getUser().id.toString(), param);
        return JsonResult.ok(true);
    }


    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        chargingPileLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出信息")
    @GetMapping("/export")
    public void export(ChargingPileQuery query, HttpServletResponse response) {
        List<ChargingPile> list = chargingPileLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "", "sheet1", ChargingPile.class, "", response);
    }

}
